//
//  DUMenuButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/11/2.
//

import Cocoa

@IBDesignable
class DUMenuButton: BaseView {

    private var imageView:NSImageView!
    private var label:DULabel!
    private var imageNamed:String = ""
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize () {
        self.mouseDownStyle = getDownStyle()
        self.mouseEnteredStyle = getEnteredStyle()
        self.defaultStyle = ViewStyleUtil.dayDefaultStyle()
        imageView = NSImageView()
        imageView.frame = NSRect(x: 3, y: 20, width: self.frame.width - 3, height: self.frame.width - 3)
        imageView.image = NSImage(named: "typeIcon")
        imageView.alphaValue = 0.7
        addSubview(imageView)
        label = DULabel()
        label.frame = NSRect(x: 0, y: 0, width: self.frame.width, height: 20)
        label.cell = VerticallyCenteredTextFieldCell()
        label.alignment = .center
        label.textColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.9)
        label.font = NSFont.systemFont(ofSize: 10)
        label.stringValue = "节日类型"
        self.baseLabel = label
        self.mouseDownHandler = mouseDownStyleClick
        self.mouseExitHandler = mouseExitStyleClick
        addSubview(label)
    }

    private func mouseDownStyleClick() {
        if let im = NSImage(named: "\(self.imageNamed)_click") {
            imageView.image = im
        }
    }
    
    private func mouseExitStyleClick() {
        imageView.image = NSImage(named: self.imageNamed)
    }
    
    func labelHidden(hidden: Bool) {
        if hidden {
            imageView.setFrameOrigin(NSPoint(x: 3, y: 3))
        }
        label.isHidden = hidden
    }
    
    private func getDownStyle() -> ViewStyle {
        var s = ViewStyle()
        s.backgroundColor = CGColor(red: 0.91, green: 0.91, blue: 0.91, alpha: 0.9)
        s.borderColor = nil
        s.borderWidth = 0
        s.radius = 0
        s.textColor = NSColor(red: 0.45, green: 0.69, blue: 0.86, alpha: 1)
        return s
    }
    
    private func getEnteredStyle() -> ViewStyle {
        var s = ViewStyle()
        s.backgroundColor = CGColor(red: 0.91, green: 0.91, blue: 0.91, alpha: 0.6)
        s.borderColor = nil
        s.borderWidth = 0
        s.radius = 0
        return s
    }
    
    func stringValue(_ value:String) {
        self.label.stringValue = value
    }
    
    func image(_ named:String) {
        self.imageNamed = named
        self.imageView.image = NSImage(named: named)
    }
    
}
